package com.orangelife.mobile.parkingcard.activity;

/**
 * 选择小区
 */
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ListView;
import android.widget.TextView;

import com.curry.android.http.HttpUtil;
import com.curry.android.http.OrangeDataHandler;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.DialogHelper;
import com.curry.orangelife.mobile.R;
import com.google.gson.reflect.TypeToken;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.constants.ParkingCardURL;
import com.orangelife.mobile.parkingcard.adapter.CommunityAdapter;
import com.orangelife.mobile.util.JsonReponseHandler;
import com.orangelife.mobile.util.ToastHelper;

import org.json.JSONObject;

public class ParkingTypeActivity extends OrangeLifeBaseActivity {
	
	/**
	 * 标题
	 */
	private TextView tvTitle;
	
	/**
	 * 小区列表
	 */
	private ListView lvList;
	
	private CommunityAdapter adapter;
	
	private List<Map<String,Object>> list;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_parking_card_building_bt);
		initeView();
		findView();
	}
	
	/**
	 * 初始化
	 */
	private void initeView() {
		
		tvTitle = (TextView) findViewById(R.id.tv_title);
		lvList = (ListView) findViewById(R.id.lvList);
	}

	/**
	 * 赋值
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void findView() {
		
		tvTitle.setText(R.string.parking_type);
		list = new ArrayList<Map<String,Object>>();
		Intent intent = getIntent();
		String commID = intent.getStringExtra("commID");
		
		adapter = new CommunityAdapter(this, list, 3);
		lvList.setAdapter(adapter);
		
		getParkingType(commID);
	}

	/***
	 * 获取停车类型列表
	 */
//	@SuppressWarnings("deprecation")
//	private void getParkingType(String commID) {
//
//		Map<String, Object> map = new HashMap<String, Object>();
//		map.put("comID", commID);
//		Map<String, Object> rootMap = new HashMap<String, Object>();
//		rootMap.put("map", map);
////		rootMap.put("url", Constant.URL_PARKING_TYPE);
//		rootMap.put("url", ParkingCardURL.PARKING_TYPE);
//		rootMap.put("wat", 0);
//		new JSONRequest(JSONRequest.GET, rootMap, handler, HttpUtil.ACTION_URI);
//	}
	@SuppressWarnings("deprecation")
	private void getParkingType(String commID) {
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("comID", commID);
		VolleyHttpUtil.getInstance().getWithCallback(ParkingCardURL.PARKING_TYPE,map, new VolleyHttpUtil.ResponseCallBack() {
			@Override
			public void handleResponse(JSONObject jsonObject, int i) {
				list=OrangeDataHandler.getListFromJsonWithPageEntity(jsonObject,new TypeToken<List<Map<String, Object>>>() {
				}.getType(),adapter,dialog);
			}
		});
	}
	
	/**
	 * 处理获得的数据
	 */
	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {

		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				list = JsonReponseHandler.getListFromJson(
						String.valueOf(msg.obj), adapter, dialog);
				break;
			case 500:
				DialogHelper.closeDialog(dialog);
				ToastHelper.getInstance()._toast(
						msg.obj.toString());
				break;
			case 1011:
				isLogin(dialog);
				break;

			default:
				break;
			}
		}

	};

}
